package com.charging.business.media;

import com.charging.business.utils.ToolsUtil;

/**
 * CPC卡计费信息文件(DF01/EF04)的编码信息
 * @author lzj
 */
public class CpcEf04EdModel {
    public static final int MSG_LEN = 512;

    /**
     * 过省信息(1-506,506,bn)
     * 每条过省信息由23个字节组成,详见PassInfoED描述, 最多存储22条过省信息
     */
    public CpcProvEdModel[] provInfos = new CpcProvEdModel[22];

    /**
     * 备用(507-512,6,bn)
     */
    public byte[] spare = new byte[6];

    //构造函数
    public CpcEf04EdModel() {
        for (int aa = 0; aa < provInfos.length; aa++) {
            provInfos[aa] = new CpcProvEdModel();
        }
        spare = ToolsUtil.hex2bin(6, "FFFFFFFFFFFF");
    }

    /**
     * 标准站文件长度检查
     * 不能为null
     * 可以为空, 为空表示单省通行, 计费以DFEF02中的内容为准
     * 其他长度必须是CPCPROVED长度的整数倍
     *
     * @param strHex
     * @return
     */
    public static boolean chkLenST(String strHex) {
        if (strHex != null) {
            // 为空,单省通行,计费以DFEF02中的内容为准
            if (strHex.isEmpty()) {
                return true;
            } else {
                int cnt = strHex.length() / (CpcProvEdModel.MSG_LEN * 2);
                int rem = strHex.length() % (CpcProvEdModel.MSG_LEN * 2);
                return cnt > 0 && rem == 0;
            }
        }
        return false;
    }

    public static CpcEf04EdModel parse(byte[] buff) {
        int index = 0;

        CpcEf04EdModel ret = new CpcEf04EdModel();

        int cnt = buff.length / CpcProvEdModel.MSG_LEN;
        if (cnt > ret.provInfos.length) {
            cnt = ret.provInfos.length;
        }

        for (int aa = 0; aa < cnt; aa++) {
            ret.provInfos[aa] = CpcProvEdModel.parse(buff, index);
            index += CpcProvEdModel.MSG_LEN;
        }
        //index +=ZTools.arrcopy(buff, index, ret.spare);

        return ret;
    }

    public static CpcEf04EdModel parse(String strHex) {
        if (strHex == null) {
            return null;
        }
        if (strHex.length() < CpcProvEdModel.MSG_LEN * 2) {
            return null;
        }
        if (!ToolsUtil.chkHexChar(strHex)) {
            return null;
        }
        byte[] buff = ToolsUtil.hex2bin(strHex);
        return parse(buff);
    }

    /**
     * 标准站文件解析
     * 为了防止程序崩溃,做如下特殊处理：
     * strHex为null, 返回初始对象
     * strHex长度不满足一体分省信息长度, 返回初始对象
     * strHex含有非法字符, 返回初始对象
     *
     * @param strHex
     * @return
     */
    public static CpcEf04EdModel parseST(String strHex) {
        if (strHex == null || strHex.isEmpty()) {
            return new CpcEf04EdModel();
        }
        if (strHex.length() < CpcProvEdModel.MSG_LEN * 2) {
            return new CpcEf04EdModel();
        }
        if (!ToolsUtil.chkHexChar(strHex)) {
            return new CpcEf04EdModel();
        }
        byte[] buff = ToolsUtil.hex2bin(strHex);
        return parse(buff);
    }

    /**
     * 信息解码
     *
     * @return
     */
    public CpcEf04Model decoded() {
        CpcEf04Model dd = new CpcEf04Model();
        for (int aa = 0; aa < provInfos.length; aa++) {
            dd.provInfos[aa] = provInfos[aa].decoded();
        }

        return dd;
    }
}
